/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2017-07-21 11:42:21
 * @Copyright: 2017 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
package com.itttq.gshop.pbpm.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.itttq.gshop.domain.po.SysFloorImgPo;
import com.itttq.gshop.domain.po.SysFloorPo;
import com.itttq.gshop.domain.to.FloorTo;
import com.itttq.gshop.domain.to.ResultPageTo;
import com.itttq.gshop.generic.util.Constants;
import com.itttq.gshop.pbpm.mapper.ISysFloorImgMapper;
import com.itttq.gshop.pbpm.mapper.ISysFloorMapper;

@Service
public class SysFloorService {
	@Autowired
	private ISysFloorMapper floorMapper;
	@Autowired
	private ISysFloorImgMapper floorImgMapper;
	
	/**
	 * 根据ID获取SysFloorPo对象
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return SysFloorPo
	 */
	public SysFloorPo getSysFloor(int id) {
		return floorMapper.findSysFloorById(id);
	}
	
	/**
	 * 根据条件获取SysFloorPo对象，并返回查询的数据总数
	 * 
	 * @param floor
	 * @Author 迷蝶
	 * @return ResultPageTo<SysFloorPo>
	 */
	public ResultPageTo<SysFloorPo> getSysFloors(SysFloorPo floor) {
		return new ResultPageTo<SysFloorPo>(floor.getStart(), floor.getSize(), 
				floorMapper.findSysFloorCount(floor), floorMapper.findSysFloors(floor));
	}
	
	/**
	 * 根据ID删除SysFloorPo对象
	 * 
	 * @param id
	 * @Author 迷蝶
	 */
	public Integer delSysFloor(int id) {
		return floorMapper.delSysFloorById(id);
	}
	
	/**
	 * 增加SysFloorPo对象
	 * 
	 * @param floor
	 * @Author 迷蝶
	 */
	public Integer addSysFloor(SysFloorPo floor) {
		return floorMapper.addSysFloor(floor);
	}
	
	/**
	 * 根据条件更新SysFloorPo对象
	 * 
	 * @param floor
	 * @Author 迷蝶
	 */
	public Integer updateSysFloor(SysFloorPo floor) {
		return floorMapper.updateSysFloor(floor);
	}
	
	/**
	 * 获取首页所有楼层以及楼层图片数据<br>
	 * 如有栏目楼层，则在楼层表增加一个分类来做
	 * 
	 * @Author 迷蝶
	 * @return List<FLoorTo>
	 */
	public List<FloorTo> getFloorsAndImgs() {
		SysFloorPo floor = new SysFloorPo();
		floor.setStatus(Constants.STATUTS_OPEN);
		floor.setSortMethod("order");
		List<SysFloorPo> floors = floorMapper.findSysFloors(floor);
		if(null == floors || floors.isEmpty()) {
			return null;
		}
		// 设置到orderTo的集合中
		List<FloorTo> floorTos = new ArrayList<FloorTo>();
		List<Integer> floorIds = new ArrayList<Integer>();
		for(SysFloorPo sfp : floors) {
			floorIds.add(sfp.getId());
			FloorTo ft = new FloorTo();
			BeanUtils.copyProperties(sfp, ft);
			floorTos.add(ft);
		}
		// 获取所有楼层的楼层图片数据
		List<SysFloorImgPo> floorImgs = floorImgMapper.findFloorImgsByFloorIds(floorIds);
		for(SysFloorImgPo sfip : floorImgs) {
			for(FloorTo sft : floorTos) {
				if(sft.getId().intValue() == sfip.getFloorId().intValue()) {
					List<SysFloorImgPo> imgs = sft.getFloorImgs();
					if(null == imgs) {
						imgs = new ArrayList<SysFloorImgPo>();
					}
					imgs.add(sfip);
					sft.setFloorImgs(imgs);
					break;
				}
			}
		}
		return floorTos;
	}
}
